from pathlib import  Path
import shutil
import random
import string

newpath = Path() / 'a/b/c/d'
path_a = Path() / 'a'
#如果当前目录存在a目录则删除
if path_a.is_dir:
    shutil.rmtree(path_a,ignore_errors=True)
#当前目录下创建a/b/c/d目录
newpath.mkdir(parents=True,exist_ok=True)
#定义批量创建文件的函数
def create_file(path,number):
    i = 1
    while i < number:
        filename = path / ''.join(random.sample(string.ascii_lowercase,4))
        with open(filename,'w') as f:
            pass
        i += 1

#d目录下创建50个文件
create_file(newpath,50)
#d目录的父目录创建50个文件
for p_path  in  newpath.parents:
    if p_path != Path():
        create_file(p_path,50)

#定义ignore函数
def ignore(src,names):
    ig = set(filter(lambda x:not x.startswith(('x','y','z')),names))
    for i in names:
        if len(i) == 1:
            ig.remove(i)
    return ig

srcpath = Path() / 'a'
dstpath = Path() / 'dst'
#拷贝前当前目录下如果存在dst目录则删除
if dstpath.is_dir():
    shutil.rmtree(dstpath)

shutil.copytree(srcpath,dstpath,ignore=ignore)


# 做得不错
